/**
 * HBEC公共JS
 */
(function ($, hb) {
  'use strict';
  var app = {
    init: function () {
      this._initCookie();
      this._initHost();
      this.renderUI();
      this.bindUI();

    },
    //初始化cookie的值
    _initCookie: function () {
      hb.session = {
        userId: null,
        userName: null
      };
      var v = $.cookie('_d_');
      if (v) {
        var i = v.indexOf(',');
        if (i > 0) {
          hb.session.userId = v.substring(0, i);
          if (i != v.length) {
            hb.session.userName = v.substring(i + 1);
          }
        }
      }
      hb.global.succ = 1;
      hb.global.fail = 0;
    },

    /**
     * 初始化host相关信息
     * @private
     */
    _initHost: function () {
      var location = window.location;
      hb.global.host = {
        host: location.host,
        port: location.port,
        protocol: location.protocol,
        url: location.protocol + '//' + location.host + '/'
      }
    },

    /**
     * 初始化表单验证
     * @private
     */
    _initValidate: function () {
      var forms = $('form[data-validate]');
      if (forms.length > 0) {
        //当有需要校验的表单时，才去加载表单校验的插件
        require(['form-validate'], function () {
          //初始化表单校验规则
          hb.validator.init();
          forms.each(function () {
            $(this).validate({
              errorElement: 'span',
              errorPlacement: function (error, element) {
                error.appendTo(element.parents('.form-group').find('.tipsInfo')).addClass('error');
                error.prev('span.hb-icons').addClass('error');
              },
              success: function (label) {
                var iconSpan = label.prev('span.hb-icons');
                iconSpan.removeClass('error');
                iconSpan.addClass('success');
                label.text('');
              },
              error: function (error) {
                error.prev('span.hb-icons').removeClass('success').addClass("error");
              }
            });
          });
        })
      }
    },

    /**
     * 初始化日期控件
     * @private
     */
    _initDatePicker: function () {
      var $dates = $('input[data-datepicker]');
      if ($dates.length > 0) {
        require(['datepicker'], function () {
          $dates.each(function () {
            var val = $(this).data('datepicker-format');
            if (val) {
              $(this).datepicker({
                dateFormat: val
              });
            } else {
              $(this).datepicker();
            }
          });
        });
      }
    },

    /**
     * 初始化城市级联控件
     * @private
     */
    _initProvinceCity: function () {
      var $city = $('select[data-city]');
      if ($city.length > 0) {
        require(['provinceCity'], function () {
          $city.each(function () {
            var name = $(this).data('group-name');
          });
        });
      }
    },

    /**
     * 初始化checkbox的全选&反选功能
     * @private
     */
    _initCheckAll: function () {
      var $checkAll = $('input[data-checkall]');
      $checkAll.bind("click", function () {
        var target = $(this);
        var name = target.attr("name");
        $("input[type='checkbox'][name='" + name + "']").prop("checked", target.prop("checked"));
      });
    },

    /**
     * 初始化数字输入的限制
     * @private
     */
    _initNumeric: function () {
      var $input = $('input[data-number]');
      if ($input.length > 0) {
        hb.number.init($input);
      }
    }
  };

  //渲染页面
  app.renderUI = function () {
    console.log('render UI');
  };

  //绑定事件
  app.bindUI = function () {
    $(document).ready(function () {
      app._initValidate();
      app._initDatePicker();
      app._initCheckAll();
      app._initNumeric();
    });
  };

  app.logic = {};
  app.init();
})(jQuery, HB);